<?php
use Helper\Controller\UploadController;
use Admin\Controller\SettingController;

/**
 * 获取上传配置
 * @return array
 */
function x_get_upload_setting() {
    $upload_setting = x_get_config_by_key(SettingController::UPLOAD_KEY);
    if (empty($upload_setting)) { //为空,则使用Common/config中默认
        $upload_setting = C('X_UPLOAD_SETTING');
    }

    $upload_setting_cache = F("setting_key_upload_setting");
    if (!empty($upload_setting_cache['upload_max_filesize'])) {
        return $upload_setting_cache;
    }

    $upload_max_filesize_setting = array();
    foreach ($upload_setting as $setting) {
        $extensions = explode(',', trim($setting['extensions']));
        if (empty($extensions)) {
            continue;
        }
        $upload_max_filesize = intval($setting['upload_max_filesize']) * 1024;//转化成KB
        foreach ($extensions as $ext){
            if (!isset($upload_max_filesize_setting[$ext]) ||
                $upload_max_filesize > $upload_max_filesize_setting[$ext] * 1024) {
                $upload_max_filesize_setting[$ext] = $upload_max_filesize;
            }
        }
    }

    $upload_setting['upload_max_filesize'] = $upload_max_filesize_setting;
    F('setting_key_upload_setting', $upload_setting);

    return $upload_setting;
}


/**
 * 获取图片预览链接
 * @param string $file 文件路径，相对于upload
 * @param string $style 图片样式，只有七牛可以用
 * @return string
 */
function x_get_image_preview_url($file, $style='watermark'){

    $url= x_get_file_upload_path($file,false);
    if (C('FILE_UPLOAD_TYPE') == UploadController::STORAGE_QINIU) {
        $storage_setting = x_get_config_by_key(SettingController::STORAGE_KEY);
        $qiniu_setting = $storage_setting['Qiniu']['setting'];
        if ($qiniu_setting['enable_picture_protect']) {
            $url = $url . $qiniu_setting['style_separator'] . $qiniu_setting['styles'][$style];
        }
        return $url;
    }
}

/**
 * 获取文件下载链接
 * @param string $file
 * @param int $expires
 * @return string
 */

function x_get_file_download_url($file, $expires=3600) {

    //此链接为正常访问的对外链接
    $url = x_get_file_upload_path($file, false);

    if (C('FILE_UPLOAD_TYPE') == UploadController::STORAGE_QINIU) {
        $storage_setting = x_get_config_by_key(SettingController::STORAGE_KEY);
        $qiniu_setting   = $storage_setting['Qiniu']['setting'];
        // 当空间为私有时,需要加密访问
        if ($qiniu_setting['enable_picture_protect']) {
            $qiniuStorage = new \Think\Upload\Driver\Qiniu\QiniuStorage(C('UPLOAD_TYPE_CONFIG'));
            $url = $qiniuStorage->privateDownloadUrl($url, $expires);
        }
    }
    return $url;
}

/**
 * 转化数据库保存的文件路径，为可以访问的url
 * @param string $file
 * @param mixed $style  样式(七牛)
 * @return string
 */
function x_get_file_upload_path($file, $style = '') {
    if (strpos($file, "http") === 0 || strpos($file, 'https') === 0 || strpos($file, "/") === 0) {
        return $file;
    } else {
        $filepath = C("TMPL_PARSE_STRING.__UPLOAD__") . $file;
        if (C('FILE_UPLOAD_TYPE') == UploadController::STORAGE_LOCAL) {
            if (strpos($filepath, 'http') !== 0 || strpos($file, 'https') === 0) {
                $filepath = x_get_host() . $filepath;
            }
        }

        if (C('FILE_UPLOAD_TYPE') == UploadController::STORAGE_QINIU) {
            $storage_setting = x_get_config_by_key(SettingController::STORAGE_KEY);
            $qiniu_setting   = $storage_setting['Qiniu']['setting'];
            $filepath = $qiniu_setting['protocol'] . '://' .
                $storage_setting['Qiniu']['domain'] . "/" . $file . $style;
        }
        return $filepath;
    }
}


/**
 * 获取文件扩展名
 * @param string $filename
 * @return string
 */
function x_get_file_extension($filename) {
    $pathinfo = pathinfo($filename);
    return strtolower($pathinfo['extension']);
}

/**
 * 安全删除文件
 *
 * @param string $file 文件名,不含路径
 * @return bool
 */
function x_delete_file($file) {

    //传入文件名必须是相对路径
    if (empty($file) || strpos($file, 'http') ===0 || strpos($file, 'https') ===0
        || strpos($file, "/") === 0) {
        return false;
    }

    //本地
    if (C('FILE_UPLOAD_TYPE') == UploadController::STORAGE_LOCAL) {
        $filepath = substr(C("TMPL_PARSE_STRING.__UPLOAD__") . $file , 1);
        //例: Uploads/attachments/20161227/5861e0d900bbf.doc 注意开头无/
        if (\Think\Storage::has($filepath) && \Think\Storage::unlink($filepath)) {
            return true;
        }
    }

    if (C('FILE_UPLOAD_TYPE') == UploadController::STORAGE_QINIU) {
        $qiniuStorage = new \Think\Upload\Driver\Qiniu\QiniuStorage(C('UPLOAD_TYPE_CONFIG'));
        if ($qiniuStorage->del($file) !== false)
         return true;
    }

    return false;
}